#include<bits/stdc++.h>
using namespace std;
int n;
string s[1010];
int main()
{
	freopen("network.in","r",stdin);
	freopen("network.out","w",stdout);
	cin>>n;
	for(int aaa=1;aaa<=n;aaa++)
	{
		string zq,zq2;
		int s1=0,s2=0,zq3[6000]={0,0,0,0,0,0},t1=1;
		
		cin>>zq;
		cin>>zq2;
		
		for(int i=0;i<zq2.size();i++)
		{
			if(zq2[i]=='.')
			{
				s1++;	
				t1++;
			}
			else if(zq2[i]==':')
			{
				s2++;
				t1++;
			}
			else if( zq2[i]=='0' && (zq2[i-1]=='.' || zq2[i-1]==':') && zq2[i+1]!='.' && zq2[i+1]!=':' )
				s2=114514;
			else
				zq3[t1] = zq3[t1]*10 + zq2[i]-'0';
					
		}
		if(s1!=3 || s2!=1 || !(zq2[0]>='0' && zq2[0]<='9'))
		{
			cout<<"ERR"<<endl;
			continue;
		}
		int f=0;
		for(int i=1;i<=4;i++)
		{
			if(zq3[i]<0 || zq3[i]>255)
			{
				f=1;
				break;
			}
		}
		if(f==1 || zq3[5]<0 || zq3[5]>65535)
		{
			cout<<"ERR"<<endl;
			continue;
		}
		
		if(zq[0]=='S')
		{
			f=0;
			for(int i=1;i<=n;i++)
			{
				if(zq2==s[i])
				{
					f=1;
					break;
				}
			}
			if(f==1)
			{
				cout<<"FAIL"<<endl;
				continue;
			}
			cout<<"OK"<<endl;
			s[aaa]=zq2;
			continue;
		}
		
		if(zq[0]=='C')
		{
			f=0;
			for(int i=1;i<=n;i++)
			{
				if(zq2==s[i])
				{
					f=i;
					break;
				}
			}
			if(f==0)
			{
				cout<<"FAIL"<<endl;
				continue;
			}
			cout<<f<<endl;
			continue;
		}
	}
	return 0;
}
